package com.android.delivery.activity.siji

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.android.delivery.R
import com.android.delivery.api.ApiService
import com.android.delivery.base.BaseActivity
import com.android.delivery.callback.DialogCallback
import com.android.delivery.callback.LzyResponse
import com.android.delivery.constant.Constants
import com.android.delivery.entity.Scan
import com.darsh.multipleimageselect.helpers.Constants.REQUEST_CODE
import com.lzy.okgo.model.Response
import com.tbruyelle.rxpermissions3.RxPermissions
import com.uuzuche.lib_zxing.activity.CaptureActivity
import com.uuzuche.lib_zxing.activity.CodeUtils
import kotlinx.android.synthetic.main.activity_sweep_code.*


/**
 * @author: mqwang
 * @create: 3/17/21 7:55 AM
 * @Description: 扫码上车
 */
class SweepCodeActivity : BaseActivity() {

    private var result: String? = "123"
    private var type: Int = 0

    companion object {
        fun self(context: Context?, type: Int) {
            val intent = Intent(context, SweepCodeActivity::class.java)
            intent.putExtra(Constants.TYPE, type)
            context?.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_sweep_code
    }

    override fun init(savedInstanceState: Bundle?, container: View?) {

        type = intent.getIntExtra(Constants.TYPE, 0)

        when (type) {
            1 -> {
                setMainTitle("扫码上车")
            }
            2 -> {
                setMainTitle("扫码下车")
            }
        }
        iv_scan.setOnClickListener {
            RxPermissions(this).request(Manifest.permission.CAMERA).subscribe { aBoolean ->
                if (aBoolean) {
                    val intent = Intent(this@SweepCodeActivity, CaptureActivity::class.java)
                    startActivityForResult(intent, REQUEST_CODE)
                }
            }
        }
        getCode()
    }

    fun getCode() {
        ApiService.transport_scan(this, result.toString(), type, object : DialogCallback<LzyResponse<Scan>>(this) {
            override fun onSuccess(response: Response<LzyResponse<Scan>>?) {
                tv_scjs.setText(response!!.body().data.currentQuantity.toString())
                tv_jrjs.setText(response!!.body().data.todayQuantity.toString())
                tv_no.setText(response!!.body().data.logisticsNo)

                tv_start_node.setText(response!!.body().data.sourceLocationName)
                tv_receiving_node.setText(response!!.body().data.targetLocationName)

                when (type) {
                    1 -> {
                        tv_status.setText("上车成功")
                    }
                    2 -> {
                        tv_status.setText("下车成功")
                    }

                }
                //1速配2零担
                when (response.body().data.logisticsType) {
                    1 -> {
                        tv_status.setText("速配")
                    }
                    2 -> {
                        tv_status.setText("零担")
                    }
                }
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        /**
         * 处理二维码扫描结果
         */
        if (requestCode === REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (resultCode == Activity.RESULT_OK) {
                val bundle: Bundle = data!!.getExtras() ?: return
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    result = bundle.getString(CodeUtils.RESULT_STRING)
                    Toast.makeText(this, "解析结果:$result", Toast.LENGTH_LONG).show()
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(this@SweepCodeActivity, "解析二维码失败", Toast.LENGTH_LONG).show()
                }
            }
        }
    }

    fun getData() {


    }
}